package pharao.client.views.game.login;

import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.view.GuiView;
import pharao.client.dto.UserDTO;
import pharao.client.i18ln.GuiText;
import pharao.client.views.game.login.events.SetLoginUserEvent;
import pharao.client.views.game.navigation.GameNavigationModel;
import pharao.client.views.navigation.PharaoNavigationModel;
import pharao.client.views.navigation.events.AddGameNavigationEvent;

import client.rpc.HibernateRemote;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public class LoginView extends GuiView {
	
	/**
	 * enthält das Username-Widget
	 */
	private TextBox textBoxUsername = new TextBox();
	
	/**
	 * enthält das Password-Widget
	 */
	private PasswordTextBox textboxPassword = new PasswordTextBox();
	
	/**
	 * enthält den LoginButton
	 */
	private Button buttonLogin = new Button();

	public void initImpl() {
		
		buttonLogin.setText(GuiText.constants.button_login());
		buttonLogin.setStylePrimaryName("buttonLogin");
		
		buttonLogin.addClickListener(new ClickListener() {
			
			public void onClick(Widget sender) {
				if (sender == buttonLogin) {
					loginAction();
				}
			}
		});
		
		textBoxUsername.setMaxLength(12);
//		textboxPassword.set TODO maxLength setzen
		
		initLoginPanel();
	}

	public void handleEventImpl(IHmvcEvent event) {
	}
	
	private void initLoginPanel() {

		buttonLogin.setEnabled(false);
		textboxPassword.setText(null);
		
		FlexTable flexTable = new FlexTable();
		flexTable.setStylePrimaryName("tableLogin");
		
		flexTable.setText(0, 0, GuiText.constants.link_login_username());
		flexTable.setText(1, 0, GuiText.constants.link_login_password());
		
		flexTable.setWidget(0, 1, textBoxUsername);
		flexTable.setWidget(1, 1, textboxPassword);
		
		flexTable.setWidget(2, 1, buttonLogin);
		
		/*
		 * Anzeigen löschen
		 */
		clear();
		
		/*
		 * neue FlexTable anzeigen
		 */
		add(flexTable);
		
		/*
		 * Button aktivieren
		 */
		buttonLogin.setEnabled(true);
	}
	
	private void loginAction() {

//		
		
		HibernateRemote.Util.getInstance().getEntities(false, "pharao.server.domain.User", 1, 1, null, true, new String[] { "name" , "password" }, new String[] { textBoxUsername.getText(), textboxPassword.getText() }, null, new AsyncCallback() {
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			
			public void onSuccess(Object result) {
				loginActionAsync(result);
			}
		});
	}
	
	private void loginActionAsync(Object result) {
		
		if (result instanceof UserDTO) {
			UserDTO userDTO = (UserDTO) result;
			
			/*
			 * User in der GameNavigationController sichern
			 */
			getController().fireEvent(new AddGameNavigationEvent(), true, false, false);
			getController().fireEvent(new SetLoginUserEvent(userDTO), true, false, false);
			
			/*
			 * Spiel-Menü anzeigen
			 */
			History.newItem(GameNavigationModel.linkGameMenu);
			
		} else {
			
			/*
			 * Nicht eingeloggt
			 */
			boolean b = Window.confirm(GuiText.constants.login_notLoggedIn());
			
			if (b) {
				/*
				 * LoginPanel erneut anzeigen
				 */
				initLoginPanel();
			} else {
				/*
				 * Index-Seite anzeigen
				 */
				History.newItem(PharaoNavigationModel.linkIndex);
			}
		}
	}

	public void terminate() {
		// TODO Auto-generated method stub
		
	}

}
